package euler.p001_050;

import euler.MainEuler;

public class Euler046 extends MainEuler {
    /*
        It was proposed by Christian Goldbach that
        every odd composite number can be written
        as the sum of a prime and twice a square.

        9 = 7 + 2×1^2
        15 = 7 + 2×2^2
        21 = 3 + 2×3^2
        25 = 7 + 2×3^2
        27 = 19 + 2×2^2
        33 = 31 + 2×1^2

        It turns out that the conjecture was false.

        What is the smallest odd composite that cannot
        be written as the sum of a prime and twice a square?

     */
    public String resolve() {

        for (int i = 35; i < Integer.MAX_VALUE; i+=2) {
            if (!primeHelper.isPrime(i)) {
                boolean sePuede = false;

                int maxJ = (int)Math.sqrt((i - 1)/2);

                for (int j = 1; j <= maxJ; j++) {
                    if (primeHelper.isPrime(i - 2*j*j)) {
                        sePuede = true;
                        break;
                    }
                }

                if (!sePuede) {
                    return String.valueOf(i);
                    // 5777
                }
            }
        }

        return null;
    }

}
